package vistasCasaCentral;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

import RMIClient.RMIClienteCC;
import beans.ListaPreciosBean;
import beans.ProveedorBean;
import parserXML.ListaPreciosXStream;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JFrameAltaListaPrecios extends javax.swing.JInternalFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	private static final long serialVersionUID = 1L;
	private JTextField txtCUIT;
	private JLabel lblCUIT;
	private JLabel jLabel3;
	private JFileChooser jfcXML;
	private JButton jButton2;
	private JButton jButtonXML;
	private JLabel jLabelXML;
	private JButton jButton3;
	private File file;
	private static JFrameAltaListaPrecios instancia;
	/**
	* Auto-generated main method to display this 
	* JInternalFrame inside a new JFrame.
	*/
		
	public JFrameAltaListaPrecios() {
		super();
//		JFrame frame = new JFrame( "Base frame" );
		initGUI();
	}
	
	
	public static JFrameAltaListaPrecios getInstancia(){
		if (instancia==null){
			instancia = new JFrameAltaListaPrecios();
		}
		return instancia;
	}
	
	private void initGUI() {
		try {
			
			this.setPreferredSize(new java.awt.Dimension(600, 225));
			this.setBounds(0, 0, 600, 225);
			setVisible(true);
			getContentPane().setLayout(null);
			this.setTitle("Cargar Lista Precios");
			this.setSize(600, 225);
			{
				lblCUIT = new JLabel();
				getContentPane().add(lblCUIT);
				lblCUIT.setText("CUIT Proveedor: ");
				lblCUIT.setBounds(42, 36, 85, 14);
			}
			{
				txtCUIT = new JTextField();
				getContentPane().add(txtCUIT);
				txtCUIT.setBounds(179, 33, 143, 21);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("Lista de Precios:");
				jLabel3.setBounds(44, 76, 89, 14);
			}
			{
				jLabelXML = new JLabel();
				getContentPane().add(jLabelXML);
				jLabelXML.setBounds(181, 73, 160, 22);
				jLabelXML.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
			}
			{
				jButtonXML = new JButton();
				getContentPane().add(jButtonXML);
				jButtonXML.setText("SeleccionarXML");
				jButtonXML.setBounds(366, 69, 168, 28);
				jButtonXML.addActionListener(new ActionListener() 
				{
					
					@Override
					public void actionPerformed(ActionEvent arg0) 
					{
						jfcXML = new JFileChooser();						
						FileNameExtensionFilter filter = new FileNameExtensionFilter("xml", "xml");
						jfcXML.addChoosableFileFilter(filter);
						int ret = jfcXML.showDialog(null, "Open file");
						 if (ret == JFileChooser.APPROVE_OPTION) {
						      file = jfcXML.getSelectedFile();
						      jLabelXML.setText(file.getName());
						      
						      
						 }						 
					}
				});
			}
			{
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("Aceptar");
				jButton2.setBounds(134, 142, 134, 29);
				jButton2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						ProveedorBean proveedorBean = new ProveedorBean();
						proveedorBean.setCuit(txtCUIT.getText());
						ListaPreciosXStream parser = new ListaPreciosXStream();
						try {
							ListaPreciosBean bean  = (ListaPreciosBean) parser.fromXML(new FileInputStream(file));
							bean.setCuit(proveedorBean.getCUIT());
							System.out.print("Prov de la lista: "+bean.getCUIT());
							System.out.print("Descripcion de la lista: "+bean.getDescripcion());
							System.out.print("Fecha de caducidad: "+bean.getFechaCaducidad());
							if(RMIClientCSR.getInstancia().cargarListaPrecios(bean)){
								JOptionPane.showMessageDialog(JFrameAltaListaPrecios.getInstancia(),"Lista Guardada");
								dispose();
							}else{
								JOptionPane.showMessageDialog(JFrameAltaListaPrecios.getInstancia(),"Error al guardar la lista");
							}
							
						} catch (FileNotFoundException e) {
						e.printStackTrace();
						}
					}
				});
			}
			{
				jButton3 = new JButton();
				getContentPane().add(jButton3);
				jButton3.setText("Cancelar");
				jButton3.setBounds(306, 142, 134, 29);
				jButton3.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						dispose();
						
					}
				});
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
